<?php

namespace app\controllers\wx\services;

use app\models\order\Order;
use app\models\order\OrderLogistics;
use app\models\user\User;
use app\models\user\UserAddress;
use app\models\user\UserCommission;
use app\util\Debug;
use app\util\Request;

class Template
{
    const URL = 'http://h5.meiqianyan.com';

    public static function send($openid, $template_id, $data, $url = '')
    {
        if (!$url) {
            $url = self::URL;
        }

        $msg = [
            "touser" => $openid,
            "template_id" => $template_id,
            "url" => $url,
            "data" => $data
        ];

        $token = Wx::getAccessToken();
        $wx_url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . $token;

        $response = Request::post($wx_url, json_encode($msg));
        $response = json_decode($response, true);
        if (!isset($response['errcode']) || $response['errcode'] != 0) {
            Debug::wx('template', json_encode($response));
        }
    }

    protected static function getOpenid($user_id)
    {
        return User::find()
            ->where(['id' => $user_id])
            ->select(['openid'])
            ->scalar();
    }

    public static function register(User $user)
    {
        /*
         * 详细内容
            {{first.DATA}}
            用户名称：{{keyword1.DATA}}
            用户类型：{{keyword2.DATA}}
            注册时间：{{keyword3.DATA}}
            所属地区：{{keyword4.DATA}}
            {{remark.DATA}}
         */

        $template_id = 'Z_5NmDhnR3YEVW_iLT1GJVH6kGsmgKzdfpEpo2xJ4u0';

        $data = [
            'first' => [
                'value' => '恭喜,注册成功!',
                'color' => '#173177',
            ],
            'keyword1' => [
                'value' => $user['mobile'],
                'color' => '#173177',
            ],
            'keyword2' => [
                'value' => '手机注册',
                'color' => '#173177',
            ],
            'keyword3' => [
                'value' => date('Y-m-d H:i', strtotime($user['create_time'])),
                'color' => '#173177',
            ],
            'keyword4' => [
                'value' => '保密',
                'color' => '#173177',
            ],
            'remark' => [
                'value' => '点击查看详情',
                'color' => '#173177',
            ],
        ];

        self::send($user['openid'], $template_id, $data);
    }

    public static function order(Order $order)
    {
        /*
         * 详细内容
            {{first.DATA}}
            收件人：{{keyword1.DATA}}
            收件人电话：{{keyword2.DATA}}
            收件地址：{{keyword3.DATA}}
            快递员：{{keyword4.DATA}}
            快递员电话：{{keyword5.DATA}}
            {{remark.DATA}}
         */

        $user = User::findOne(['id' => $order->user_id]);
        // $logistics = OrderLogistics::findOne(['order_id' => $order->id]);
        $address = UserAddress::findOne(['id' => $order->user_address]);

        $template_id = 'aVsFk7-eGEuOG_Uo8gJ2YjIGHL6GO3xQNsRHs0cCAPM';

        $data = [
            'first' => [
                'value' => '您的订单已支付成功',
                'color' => '#173177',
            ],
            'keyword1' => [
                'value' => base64_decode($user['nickname']),
                'color' => '#173177',
            ],
            'keyword2' => [
                'value' => $user['mobile'],
                'color' => '#173177',
            ],
            'keyword3' => [
                'value' => $address['address'],
                'color' => '#173177',
            ],
            'keyword4' => [
                'value' => '',
                'color' => '#173177',
            ],
            'keyword5' => [
                'value' => '',
                'color' => '#173177',
            ],
            'remark' => [
                'value' => '点击查看详情',
                'color' => '#173177',
            ],
        ];

        self::send(self::getOpenid($order['user_id']), $template_id, $data);
    }

    public static function commission(UserCommission $commission)
    {
        /*
         * 详细内容
            {{first.DATA}}

            佣金金额：{{keyword1.DATA}}

            时间：{{keyword2.DATA}}
            {{remark.DATA}}

         */

        $template_id = 'cS0JNMVIqbv6NBMxZY1sURivvbrOxqRPZQFZ3e09c1w';

        $data = [
            'first' => [
                'value' => '您有佣金到帐了',
                'color' => '#173177',
            ],
            'keyword1' => [
                'value' => $commission->balance,
                'color' => '#173177',
            ],
            'keyword2' => [
                'value' => date('Y-m-d H:i', strtotime($commission['create_time'])),
                'color' => '#173177',
            ],
            'remark' => [
                'value' => '点击查看详情',
                'color' => '#173177',
            ],
        ];

        self::send(self::getOpenid($commission['user_id']), $template_id, $data);
    }
}